package com.kfgj.hkp.common.utils;

import com.kfgj.hkp.common.logging.Logger;
import com.kfgj.hkp.common.logging.LoggerFactory;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @description TODO
 * @auth liwensihan
 * @date 2018/12/28 11:15
 */
public class BeanMapConvertUtils {

    private static final Logger logger = LoggerFactory.getLogger(BeanMapConvertUtils.class);

    /**
     * 转换到对象属性到map
     *
     * @param obj
     * @return
     */
    public static Map<String, Object> convertExclude(Object obj, String... excludeProperty) {
        Map<String, Object> map = null;
        try {

            map = BeanUtilsBean.getInstance().getPropertyUtils().describe(obj);

            removeNull(map);

            for (String prop : excludeProperty) {
                if (StringUtils.isNotEmpty(prop)) {
                    map.remove(prop);
                }
            }

        } catch (IllegalAccessException e) {
            logger.error(e);
        } catch (InvocationTargetException e) {
            logger.error(e);
        } catch (NoSuchMethodException e) {
            logger.error(e);
        }

        return map;
    }

    public static Map<String, Object> convertInclude(Object obj, String... includeProperty) {
        Map<String, Object> map = new HashMap<>();
        try {

            if (includeProperty != null && includeProperty.length > 0) {
                Map<String,Object> tmp = BeanUtilsBean.getInstance().getPropertyUtils().describe(obj);

                for (String prop : includeProperty) {
                    if (StringUtils.isNotEmpty(prop)) {
                        map.put(prop,tmp.get(prop));
                    }
                }

            }

        } catch (IllegalAccessException e) {
            logger.error(e);
        } catch (InvocationTargetException e) {
            logger.error(e);
        } catch (NoSuchMethodException e) {
            logger.error(e);
        }

        return map;
    }

    /**
     * 从map中指定的key/value,填充target
     *
     * @param target
     * @param properties
     */
    public static void populate(Object target, final Map<String, ? extends Object> properties) {

        try {
            removeNull(properties);
            BeanUtils.populate(target, properties);
        } catch (IllegalAccessException e) {
            logger.error(e);
        } catch (InvocationTargetException e) {
            logger.error(e);
        }
    }

    private static void removeNull(Map<String, ? extends Object> map) {

        List<String> keys = new ArrayList<>();

        for (Map.Entry<String, ? extends Object> entry : map.entrySet()) {
            if (ObjectUtils.isEmpty(entry.getValue())) {
                keys.add(entry.getKey());
            }
        }

        for (String key : keys) {
            map.remove(key);
        }
    }


}
